<?php

namespace Home\Controller;

class ImageController extends CommonController
{
    //显示相册列表页面
    public function index()
    {
        $user_id=$_SESSION['home']['uid'];
        $albums = M('Albums');
        $albumsList = $albums->where('user_id='.$user_id)->select();
        $this->assign('albumsList', $albumsList);
        $this->display();
    }

    //相册添加处理用ajax脚本
    public function albumsadddo()
    {
        if (IS_AJAX) {
            $rules = array(
                array('albums_name', 'require', '请填写相册名称！'),
                array('albums_type', '2,4', '请选择相册类型！', 0, 'between', 1),
            );
            $data = array(
                'albums_name' => I('get.name'),
                'albums_type' => I('get.type'),
                'user_id' => $_SESSION['home']['uid'],
                'time' => $_SERVER['REQUEST_TIME'],
            );
            $albums = M('albums');
            if ($albums->validate($rules)->create($data)) {
                $id = $albums->add();
                $result = array(
                    'status' => 1,
                    'id' => $id,
                );
            } else {
                $result = array(
                    'status' => 0,
                    'info' => $albums->getError(),
                );
            }
            $this->ajaxReturn($result);
        }
    }

    //相册编辑处理用ajax脚本
    public function albumseditdo()
    {
        if (IS_AJAX) {
            $rules = array(
                array('albums_name', 'require', '请填写相册名称！'),
                array('albums_type', '2,4', '请选择相册类型！', 0, 'between', 1),
            );
            $data = array(
                'albums_id' => I('get.id'),
                'albums_name' => I('get.name'),
                'albums_type' => I('get.type'),
                'user_id' => $_SESSION['home']['uid'],
            );
            $albums = M('albums');
            if ($albums->validate($rules)->create($data)) {
                $albums->save();
                $result = array('status' => 1);
            } else {
                $result = array(
                    'status' => 0,
                    'info' => $albums->getError(),
                );
            }
            $this->ajaxReturn($result);
        }
    }

    //相册删除处理用ajax脚本    相册连同相片一起删除
    public function albumsdeldo()
    {
        if (IS_AJAX) {
            $id = I('get.id');
            $albums = M('albums');
            if ($albums->where('albums_type>1')->delete($id)) {
                $photos = M('Photos');
                //删除相片文件

                //删除相片信息
                $photos->where('albums_id='.$id)->delete();

                $result = array('status' => 1);
            } else {
                $result = array(
                    'status' => 0,
                );
            }
            $this->ajaxReturn($result);
        }
    }

    //显示对应相册中的相片列表
    public function image()
    {
        $id = I('get.id', 0, 'int');
        $photos = M('Photos');
        $photosList = $photos->where('albums_id='.$id)->select();
        $this->assign('photosList', $photosList);
        $this->display();
    }

    //修改对应相片的名称调用的ajax脚本
    public function imageeditdo()
    {
        if (IS_AJAX) {
        }
    }

    //在对应相册中添加相片的处理页面
    public function imageadddo()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/'; // 设置附件上传根目录
        $upload->savePath = 'Uploads/photo/'; // 设置附件上传目录
        // 上传文件
        $info = $upload->uploadOne($_FILES['file']);
        if ($info) {
            // 上传成功
            $photos = M('Photos');
            $data = array(
                'albums_id' => I('post.id', 0, 'int'),
                'photos_name' => $info['name'],
                'photos_info' => '',
                'photos_zan' => 0,
                'photos_path' => '/'.$info['savepath'].$info['savename'],
                'time' => $_SERVER['REQUEST_TIME'],
            );
            if ($photos->create($data)) {
                $photos->add($data);
            }
        }
    }

    //删除对应相片需要调用的ajax脚本
    public function imagedeldo()
    {
        //这里请使用ajax访问
        // if (IS_AJAX) {}
        echo '删除对应相片需要调用的ajax脚本!';
    }
}
